#ifndef COMMONHELPER_H
#define COMMONHELPER_H
#include <QApplication>
#include <QWidget>
#include <QFile>
#include <QDebug>

/*
CONFIG(release, debug|release) {
    DEFINES += QT_NO_DEBUG_OUTPUT
    DEFINES += QT_NO_INFO_OUTPUT
    DEFINES += QT_NO_WARNING_OUTPUT
} else {
    message(Debug build!)
}
*/

// 配合上述.pro文件配置，实现release模式时不输出日志
#ifndef QT_NO_DEBUG
//调试所用宏
#define xdebug   (QDebug(QtDebugMsg).nospace() << \
        "Debug: [" << Q_FUNC_INFO << " @" << __LINE__ << "] MSG:").space()
#define zdebug(...)   (QDebug(QtDebugMsg) \
        << "Debug: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";

#define xinfo   (QDebug(QtInfoMsg).nospace() << \
        "Info: [" << Q_FUNC_INFO << " @" << __LINE__ << << "] MSG:").space()
#define zinfo(...)   (QDebug(QtInfoMsg) \
        << "Info: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";

#define xwarning   (QDebug(QtWarningMsg).nospace() << \
        "Warning: [" << Q_FUNC_INFO << " @" << __LINE__ << "] MSG:").space()
#define zwarning(...)   (QDebug(QtWarningMsg) \
        << "Warning: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";

#define xcritical   (QDebug(QtCriticalMsg).nospace() << \
        "Critical: [" << Q_FUNC_INFO << " @" << __LINE__ << "] MSG:").space()
#define zcritical(...)   (QDebug(QtCriticalMsg) \
        << "Critical: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";
//注意使用fatal会使程序终止
#define xfatal   (QDebug(QtFatalMsg).nospace() << \
        "Fatal: [" << Q_FUNC_INFO << " @" << __LINE__ << "] MSG:").space()
#define zfatal(...)   (QDebug(QtFatalMsg) \
        << "Fatal: " << __VA_ARGS__ ).nospace() << '[' << Q_FUNC_INFO << " @" << __FILE__ << '(' << __LINE__ << ")]";
#else
#define xdebug qDebug()
#define xinfo qInfo()
#define xwarning qWarning()
#define xcritical qCritical()
#endif

class CommonHelper
{
public:
    static void setStyleWithQSS(const QString &qssFile) {
        QFile qss(qssFile);
        qss.open(QFile::ReadOnly);
        qApp->setStyleSheet(qss.readAll());
        qss.close();
    }
    static void setStyleWithQSS(const QString &qssFile, QWidget* pW) {
        QFile qss(qssFile);
        qss.open(QFile::ReadOnly);
        pW->setStyleSheet(qss.readAll());
        qss.close();
    }
    //qInstallMessageHandler(CommonHelper::myMessageOutput);
static void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
  {
      QByteArray localMsg = msg.toLocal8Bit();
      const char *file = context.file ? context.file : "";
      const char *function = context.function ? context.function : "";
      switch (type) {
      case QtDebugMsg:
          fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
          break;
      case QtInfoMsg:
          fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
          break;
      case QtWarningMsg:
          fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
          break;
      case QtCriticalMsg:
          fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
          break;
      case QtFatalMsg:
          fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
          break;
      }
  }
};
#endif // COMMONHELPER_H
